﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Speed;

namespace SiteTest.WebControls
{

    public class GridViewSorted : GridView
    {

        #region Properties

        string key(string name)
        {
            return Conv.GetKey(this.Page.Request.RawUrl, this.ID, name);
        }

        // TODO: Não está preservando os valores de SortDirection2 e SortExpression2 usando attributes ou viewstate.
        // Corrigir isso e tirar a Session

        [Bindable(true)]
        [Category("Data")]
        public SortDirection SortDirection2
        {
            //get { return !string.IsNullOrEmpty(this.Attributes["SortDirection2"] as string) ? (SortDirection)Convert.ToInt32(this.Attributes["SortDirection2"]) : SortDirection.Ascending; }
            //set { this.Attributes["SortDirection2"] = ((int)value).ToString(); }
            get { return !string.IsNullOrEmpty(Page.Session[key("SortDirection2")] as string) ? (SortDirection)Convert.ToInt32(Page.Session[key("SortDirection2")]) : SortDirection.Ascending; }
            set { Page.Session[key("SortDirection2")] = ((int)value).ToString(); }
        }

        [Bindable(true)]
        [Category("Data")]
        public string SortExpression2
        {
            get { return Page.Session[key("SortExpression2")] as string; }
            set { Page.Session[key("SortExpression2")] = value; }
        }
        //[Category("Data")]
        //public SortDirection SortDirection2 { get; set; }
        //[Category("Data")]
        //public string SortExpression2 { get; set; }

        public string SortSql
        {
            get
            {
                return SortDirection2 == SortDirection.Ascending ? SortExpression2 : SortExpression2 + " DESC";
            }
        }

        #endregion Properties

        #region Override

        protected override void OnPreRender(EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //SortDirection2 = SortDirection;
                //SortExpression2 = SortExpression;
            }
            base.OnPreRender(e);
        }

        protected override void OnSorting(GridViewSortEventArgs e)
        {
            if (string.IsNullOrEmpty(SortExpression2))
            {
                SortExpression2 = e.SortExpression;
                SortDirection2 = e.SortDirection;
            }
            if (SortExpression2 != e.SortExpression)
            {
                SortDirection2 = SortDirection.Ascending;
                SortExpression2 = e.SortExpression;
            }
            else
            {
                SortDirection2 = SortDirection2 == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
            }
            e.SortDirection = SortDirection2;
            e.SortExpression = SortExpression2;

            base.OnSorting(e);
        }

        #endregion Override

    }

}